﻿using System;
using System.Xml.Linq;

namespace YArchitech.Temp
{
	public class HYDuctTee : HYDuctFitting
	{
		public HYDuctSpecification SidePort
		{
			get
			{
				return this.m_SidePort;
			}
			set
			{
				this.m_SidePort = value;
			}
		}

		public double OutFlow { get; set; }

		public double SideFlow { get; set; }

		public HYMEPObject Side
		{
			get
			{
				return this.m_SideObject;
			}
		}

		public int SideObjectId
		{
			get
			{
				if (this.m_SideObject != null)
				{
					this.m_SideObjectId = this.m_SideObject.Id;
				}
				return this.m_SideObjectId;
			}
		}

		public double SideCofficient
		{
			get
			{
				if (Utility.IsZero(this.m_UserSideCoefficient, 1E-05))
				{
					return this.m_SideCoefficient;
				}
				return this.m_UserSideCoefficient;
			}
			set
			{
				this.m_SideCoefficient = value;
			}
		}

		public double Angle { get; set; }

		public double SideAngle { get; set; }

		public bool IsConfluence { get; set; }

		public string SideCoefficientShowString { get; set; }

		public HYDuctTee()
		{
			base.Name = "风管三通";
			this.m_SidePort = new HYDuctSpecification(HYShapeProfile.Rectangular, 800, 320);
			this.m_SideCoefficient = 0.0;
			this.m_UserSideCoefficient = 0.0;
			this.Angle = 0.0;
			this.SideAngle = Math.PI*.5;
			this.IsConfluence = false;
			base.CoefficientShowString = "三通直通管";
			this.SideCoefficientShowString = "三通旁通管";
		}

		public virtual void SetSideObject(HYMEPObject sideObj)
		{
			if (sideObj == null)
			{
				return;
			}
			this.m_SideObject = sideObj;
			this.m_SideObjectId = sideObj.Id;
			this.m_SideObject.Previous = this;
			this.m_SideObject.Place = HYMEPObject.Down(base.Place);
			if (this.m_SideObject is HYPipeline)
			{
				this.m_SideHostPipeline = (this.m_SideObject as HYPipeline);
			}
		}

		public virtual HYPipeline GetSideHostPipeline()
		{
			if (this.m_SideHostPipeline != null)
			{
				return this.m_SideHostPipeline;
			}
			HYMEPObject hymepobject = this.Side;
			while (hymepobject != null && !(hymepobject is HYPipeline))
			{
				hymepobject = hymepobject.Next;
			}
			if (hymepobject == null)
			{
				return null;
			}
			this.m_SideHostPipeline = (hymepobject as HYPipeline);
			return this.m_SideHostPipeline;
		}

		public override void SetCoeffient(double coef, int PartIndex = 0)
		{
			if (1 == PartIndex)
			{
				this.m_UserSideCoefficient = coef;
				return;
			}
			base.SetCoeffient(coef, PartIndex);
		}

		protected virtual double CalDirectCoefficient()
		{
			HYDuctStraight hyductStraight = this.GetHostPipeline() as HYDuctStraight;
			HYDuctStraight hyductStraight2 = this.GetSideHostPipeline() as HYDuctStraight;
			HYDuctStraight hyductStraight3 = this.GetPreviousPipeline() as HYDuctStraight;
			HYDuctSpecification hyductSpecification = hyductStraight.Section as HYDuctSpecification;
			HYDuctSpecification hyductSpecification2 = hyductStraight3.Section as HYDuctSpecification;
			if (this.IsConfluence)
			{
				return Hydrokinetics.HeLiuSanTongDragLocalIndex(hyductStraight3.Velocity, hyductStraight3.Flow, hyductStraight.Velocity, hyductStraight.Flow, hyductStraight2.Velocity, hyductStraight2.Flow, this.Angle, this.SideAngle, true);
			}
			if (Utility.IsEqual(this.Angle, this.SideAngle, 1E-05))
			{
				if (!base.IsRound)
				{
					return 0.5;
				}
				return 1.0;
			}
			else
			{
				if (base.IsRound)
				{
					return Hydrokinetics.DistributaryCLR_90Circle_S(hyductStraight3.Flow, hyductSpecification2.Area, hyductStraight.Flow, hyductSpecification.Area);
				}
				return Hydrokinetics.DistributaryCLR_90Rectangle_S(hyductStraight3.Flow, hyductSpecification2.Area, hyductStraight.Flow, hyductSpecification.Area);
			}
		}

		protected virtual double CalSideCoefficient(HYDuctStraight plSide)
		{
			HYDuctStraight hyductStraight = this.GetHostPipeline() as HYDuctStraight;
			HYDuctStraight hyductStraight2 = this.GetPreviousPipeline() as HYDuctStraight;
			if (this.IsConfluence)
			{
				return Hydrokinetics.HeLiuSanTongDragLocalIndex(hyductStraight2.Velocity, hyductStraight2.Flow, plSide.Velocity, plSide.Flow, hyductStraight.Velocity, hyductStraight.Flow, this.Angle, this.SideAngle, false);
			}
			if (Utility.IsEqual(this.Angle, this.SideAngle, 0.01))
			{
				if (!base.IsRound)
				{
					return 0.25;
				}
				return 1.0;
			}
			else
			{
				if (!base.IsRound)
				{
					return Hydrokinetics.DistributaryCLR_90Rectangle_B(hyductStraight2.Flow, hyductStraight2.Section.Area, hyductStraight.Flow, hyductStraight.Section.Area);
				}
				if (this.Angle > Math.PI*.5)
				{
					return Hydrokinetics.DistributaryCLR_45Circle_B(hyductStraight2.Flow, hyductStraight2.Section.Area, hyductStraight.Flow, hyductStraight.Section.Area);
				}
				return Hydrokinetics.DistributaryCLR_90Circle_B(hyductStraight2.Flow, hyductStraight2.Section.Area, hyductStraight.Flow, hyductStraight.Section.Area);
			}
		}

		public override void CalCoefficient()
		{
			this.m_Coefficient = 0.0;
			this.m_SideCoefficient = 0.0;
			HYDuctStraight plSide = this.GetSideHostPipeline() as HYDuctStraight;
			if (Utility.IsZero(this.m_UserCoefficient, 1E-05))
			{
				this.m_Coefficient = this.CalDirectCoefficient();
			}
			if (Utility.IsZero(this.m_UserSideCoefficient, 1E-05))
			{
				this.m_SideCoefficient = this.CalSideCoefficient(plSide);
			}
		}

		public override void Cal(HYSystemOption option)
		{
			HYPipeline hostPipeline = this.GetHostPipeline();
			HYDuctStraight hyductStraight = this.GetSideHostPipeline() as HYDuctStraight;
			if (hostPipeline == null || hyductStraight == null)
			{
				return;
			}
			this.CalCoefficient();
			base.CoefficientShowString = "【" + base.Name + "】直通管";
			this.SideCoefficientShowString = "【" + base.Name + "】旁通管";
			hostPipeline.AddAttachedObject(this, base.CoefficientShowString, base.Coefficient, false, 0);
			hyductStraight.AddAttachedObject(this, this.SideCoefficientShowString, this.SideCofficient, false, 1);
		}

		public override void RebuildIndex(ref int index)
		{
			base.RebuildIndex(ref index);
			if (this.m_SideObject != null)
			{
				this.m_SideObject.RebuildIndex(ref index);
			}
		}

		public override void ReAllocateFlow()
		{
			base.ReAllocateFlow();
			double num = 0.0;
			if (this.m_SideObject != null)
			{
				this.m_SideObject.ReAllocateFlow();
				num = this.m_SideObject.Flow;
			}
			base.Flow += num;
		}

		public override void ReadXml(XElement xe)
		{
			base.ReadXml(xe);
			this.SideCofficient = xe.GetDoubleAttribute("SideCofficient", 0.0);
			this.m_SideObjectId = xe.GetIntAttribute("SideObjectId", 0);
			XElement xe2 = xe.ChildElement("Side");
			this.m_SidePort.ReadXml(xe2);
		}

		public override void WriteXml(XElement xe)
		{
			base.WriteXml(xe);
			xe.SetAttributeValue("SideCofficient", this.SideCofficient);
			xe.SetAttributeValue("SideObjectId", this.SideObjectId);
			XElement xe2 = xe.ChildElement("Side");
			this.m_SidePort.WriteXml(xe2);
		}

		protected HYDuctSpecification m_SidePort;

		protected HYMEPObject m_SideObject;

		protected HYPipeline m_SideHostPipeline;

		protected int m_SideObjectId;

		protected double m_SideCoefficient;

		protected double m_UserSideCoefficient;
	}
}
